﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.Xml.Linq;

namespace GameTank
{
    public class WeaponDTO
    {

        public WeaponDTO()
        {
            khoiTaoMacDinh();
        }

        private void khoiTaoMacDinh()
        {
            _currentExplodingFrame = -1;
            this.IsReady = true;
            this.MoveSpeed = 10.0f;
        }

        string flyingModel = @"TankBullet.mdd";

        public string FlyingModel
        {
            get { return flyingModel; }
            set { flyingModel = value; }
        }
        string explodingModel = @"ExplodingTankBullet.mdd";

        public string ExplodingModel
        {
            get { return explodingModel; }
            set { explodingModel = value; }
        }

        Character parent;

        public Character Parent
        {
            get { return parent; }
            set { parent = value; }
        }


        private Vector2 _velocity;

        public Vector2 Velocity
        {
            get { return _velocity * _moveSpeed; }
        }


        float _moveSpeed;

        public float MoveSpeed
        {
            get { return _moveSpeed; }
            set { _moveSpeed = value; }

        }

        int _direction;  // 0 up 1 down 2 left 3 right
        public int Direction
        {
            get { return _direction; }
            set
            {
                _direction = value;
                _velocity *= 0;
                switch (_direction)
                {
                    case 0:
                        _velocity.Y = -1;
                        break;
                    case 1:
                        _velocity.Y = 1;
                        break;
                    case 2:
                        _velocity.X = -1;
                        break;

                    case 3:
                        _velocity.X = 1;
                        break;
                }
            }
        }

        Vector2 _pos;

        public Vector2 Pos
        {
            get { return _pos; }
            set { _pos = value; }
        }

        int _width;

        public int Width
        {
            get { return _width; }
            set { _width = value; }
        }

        int _hieght;

        public int Height
        {
            get { return _hieght; }
            set { _hieght = value; }
        }

        bool _flying;

        public bool IsFlying
        {
            get { return _flying; }
            set
            {
                if (value)
                {
                    _ready = false;
                    _exploding = false;
                }
                _flying = value;
            }
        }

        bool _ready;

        public bool IsReady
        {
            get { return _ready; }
            set
            {
                if (value)
                {
                    _exploding = false;
                    _flying = false;
                }
                _ready = value;
            }
        }

        bool _exploding;

        public bool IsExploding
        {
            get { return _exploding; }
            set
            {
                if (value)
                {
                    _ready = false;
                    _flying = false;
                }
                _exploding = value;
            }
        }

        int _currentExplodingFrame;

        public int CurrentExplodingFrame
        {
            get { return _currentExplodingFrame; }
            set { _currentExplodingFrame = value; }
        }

        public XElement ToXml()
        {
            XElement element = new XElement("Weapon");

            element.SetAttributeValue("flyingModel", this.flyingModel);

            element.SetAttributeValue("explodingModel", this.explodingModel);

            element.SetAttributeValue("moveSpeed", this.MoveSpeed);


            element.SetAttributeValue("direction", this.Direction);

            element.SetAttributeValue("currentExplodingFrame", this.CurrentExplodingFrame);

            element.Add(new XElement("pos", new XAttribute("x", this.Pos.X), new XAttribute("y", this.Pos.Y)));

            element.SetAttributeValue("width", this.Width);

            element.SetAttributeValue("height", this.Height);

            element.SetAttributeValue("flying", this.IsFlying);

            element.SetAttributeValue("ready", this.IsReady);

            element.SetAttributeValue("exploding", this.IsExploding);

            return element;
        }

        public static WeaponDTO Parse(XElement element)
        {
            WeaponDTO weaponDTO = new WeaponDTO();

            weaponDTO.FlyingModel = element.Attribute("flyingModel").Value;

            weaponDTO.ExplodingModel = element.Attribute("explodingModel").Value;

            weaponDTO.MoveSpeed = float.Parse(element.Attribute("moveSpeed").Value);

            weaponDTO.Direction = int.Parse(element.Attribute("direction").Value);

            weaponDTO.CurrentExplodingFrame = int.Parse(element.Attribute("currentExplodingFrame").Value);

            int x = int.Parse(element.Element("pos").Attribute("x").Value);
            int y = int.Parse(element.Element("pos").Attribute("y").Value);
            weaponDTO.Pos = new Vector2(x, y);

            weaponDTO.Width = int.Parse(element.Attribute("width").Value);

            weaponDTO.Height = int.Parse(element.Attribute("height").Value);

            weaponDTO.IsFlying = bool.Parse(element.Attribute("flying").Value);

            weaponDTO.IsReady = bool.Parse(element.Attribute("ready").Value);

            weaponDTO.IsExploding = bool.Parse(element.Attribute("exploding").Value);

            return weaponDTO;
        }

        public void SaveToFile(String fileName)
        {
            XDocument doc = new XDocument();
            doc.Add(ToXml());
            doc.Save(fileName);
        }
    }   
}
